const db = require("../db")
const { getGUID } = require("../utils")

/**
 * @class
 * @classdesc 用户模型类
 */
class UserModel {
  /**
   * @constructs UserModel
   */
  constructor({ name, age }) {
    this.id = getGUID()
    this.name = name
    this.age = age
  }

  /**
   * 模拟数据库这层用户新增
   */
  create() {
    const users = db.getData()['users']
    users.push({
      id: this.id,
      name: this.name,
      age: this.age,
    })
    db.setData('users', users)
    return this
  }

  static getUserList() {
    return db.getData()['users']
  }

  static delUser(id) {
    const users = db.getData()['users']
    console.log('users', users)
    const _users = users.filter(item => {
      console.log(item)
      console.log(typeof item.id, item.id)
      console.log(typeof id, id)
      console.log(item.id !== id)
      return item.id !== id
    })
    console.log(_users)
    db.setData('users', _users)
    return db.getData()['users']
  }
}

module.exports = UserModel